package com.atguigu.day02;

import com.atguigu.util.ClickEvent;
import com.atguigu.util.ClickSource;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class Example3 {
    public static void main(String[] args) {
        var env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new ClickSource())
                .map(r -> r.username)
                .print("lambda");

        env
                .addSource(new ClickSource())
                .map(new MapFunction<ClickEvent, String>() {
                    @Override
                    public String map(ClickEvent clickEvent) throws Exception {
                        return clickEvent.username;
                    }
                })
                .print("匿名类");

        env
                .addSource(new ClickSource())
                .map(new MyMap())
                .print("外部类");

        env
                .addSource(new ClickSource())
                .flatMap(new FlatMapFunction<ClickEvent, String>() {
                    @Override
                    public void flatMap(ClickEvent clickEvent, Collector<String> collector) throws Exception {
                        collector.collect(clickEvent.username);
                    }
                })
                .print("flatMap");

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class MyMap implements MapFunction<ClickEvent, String> {
        @Override
        public String map(ClickEvent clickEvent) throws Exception {
            return clickEvent.username;
        }
    }
}
